//
//  UINavigationItem+YJCategory.m
//  Rail
//
//  Created by hychen on 2018/2/25.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "UINavigationItem+YJCategory.h"

#define NavTitleFont 20

@implementation UINavigationItem (YJCategory)
- (void)addLeftButton:(id)view{
    [self addLeftButton:view imageNamed:@"navback"];
}

- (void)addLeftButton:(id)view imageNamed:(NSString *)imageName
{
    NSInteger bt = self.leftBarButtonItem.tag;
    if (bt == 502) {
        return;
    }
    self.leftBarButtonItem = nil;
    [self setHidesBackButton:TRUE animated:NO];
    SEL actionMethod = @selector(goBack);
    UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgButton setFrame:CGRectMake(0, 0, 35, 44)];
    [bgButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20)];
    [bgButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [bgButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
    [bgButton addTarget:view action:actionMethod forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:bgButton];
    leftItem.tag = 502;
    leftItem.title = @"";
    
    self.leftBarButtonItem = leftItem;
    self.leftBarButtonItem.title = @"";
    
    if ([view isKindOfClass:[UIViewController class]]) {
        ((UIViewController*)view).navigationController.navigationBar.backItem.title = @"";
    }
}


- (void)addSimpleTitleView:(NSString *)title {
    [self addSimpleTitleView:title FontSize:[UIFont systemFontOfSize:NavTitleFont] MaxWidth:200.0f MaxHeight:45.0f];
}

- (void)addSimpleTitleView:(NSString *)title titleColor:(UIColor *)titleColor {
    [self addSimpleTitleView:title titleColor:titleColor FontSize:[UIFont systemFontOfSize:NavTitleFont] MaxWidth:200.f MaxHeight:45.f];
}

- (void)addSimpleTitleView:(NSString *)title MaxWidth:(float)maxWidth MaxHeight:(float)maxHeight {
    [self addSimpleTitleView:title titleColor:[UIColor whiteColor] FontSize:[UIFont systemFontOfSize:NavTitleFont] MaxWidth:maxWidth MaxHeight:maxHeight];
}

- (void)addSimpleTitleView:(NSString *)title FontSize:(UIFont *)font MaxWidth:(float)maxWidth MaxHeight:(float)maxHeight{
    [self addSimpleTitleView:title titleColor:[UIColor whiteColor] FontSize:font MaxWidth:maxWidth MaxHeight:maxHeight];
}

- (void)addSimpleTitleView:(NSString *)title titleColor:(UIColor *)titleColor FontSize:(UIFont *)font MaxWidth:(float)maxWidth MaxHeight:(float)maxHeight
{
    
    UILabel * tl = nil;
    if (self.titleView && [self.titleView isKindOfClass:[UILabel class]]) {
        tl = (UILabel *) self.titleView;
        [tl setFrame:CGRectMake(self.titleView.origin.x, self.titleView.origin.y, maxWidth, maxHeight)];
        tl.textAlignment = NSTextAlignmentCenter;
        tl.text = title;
        tl.textColor = titleColor;
        [tl sizeToFit];
        [tl autoHeightWithin:maxHeight];
        tl.numberOfLines = 1;
    }else{
        
        tl = [[UILabel alloc] init];
        tl.textColor = titleColor;
        [tl setFrame:CGRectMake(0.0f, 0.0f, maxWidth, maxHeight)];
        tl.text = title;
        tl.backgroundColor = [UIColor clearColor];
        tl.font = font;
        tl.textAlignment = NSTextAlignmentCenter;
        tl.clipsToBounds = YES;
        [tl autoHeightWithin:maxHeight];
        tl.numberOfLines = 1;
        self.titleView = tl;
    }
    
}

- (void)addSimpleWebViewTitleView:(NSString *)title titleColor:(UIColor *)titleColor FontSize:(UIFont *)font MaxWidth:(float)maxWidth MaxHeight:(float)maxHeight{
    
    UILabel * tl = nil;
    if (self.titleView && [self.titleView isKindOfClass:[UILabel class]]) {
        tl = (UILabel *) self.titleView;
        [tl setFrame:CGRectMake(self.titleView.origin.x, self.titleView.origin.y, maxWidth, maxHeight)];
        tl.textAlignment = NSTextAlignmentCenter;
        tl.text = title;
        tl.textColor = titleColor;
        [tl sizeToFit];
        [tl autoHeightWithin:maxHeight];
        tl.numberOfLines = 1;
    }else{
        
        tl = [[UILabel alloc] init];
        tl.textColor = titleColor;
        [tl setFrame:CGRectMake(0.0f, 0.0f, maxWidth, maxHeight)];
        tl.text = title;
        tl.backgroundColor = [UIColor clearColor];
        tl.font = font;
        tl.textAlignment = NSTextAlignmentCenter;
        tl.clipsToBounds = YES;
        
        [tl autoHeightWithin:maxHeight];
        tl.numberOfLines = 1;
        self.titleView = tl;
    }
    
    
    CGRect leftViewbounds = self.leftBarButtonItem.customView.bounds;
    CGRect rightViewbounds = self.rightBarButtonItem.customView.bounds;
    //判断右边第一个item是否存在
    UIButton *btn = [self.rightBarButtonItem.customView viewWithTag:1003];
    CGFloat rightViewWidth = btn.currentTitle&&![btn.currentTitle isEqualToString:@""]?rightViewbounds.size.width:rightViewbounds.size.width-35;
    rightViewWidth = btn.currentImage?rightViewbounds.size.width:rightViewbounds.size.width-35;
    
    CGFloat paddingMax = leftViewbounds.size.width > rightViewWidth ? leftViewbounds.size.width : rightViewWidth;
    paddingMax += 15;//leftview 左右都有间隙，左边是5像素，右边是8像素，加2个像素的阀值 5 ＋ 8 ＋ 2
    CGRect frame = tl.frame;
    frame.size.width = SCREEN_WIDTH - paddingMax * 2;
    tl.frame = frame;

}

- (void)addRightItem:(id)owner title:(NSString *)title selector:(SEL)selector
{
    SEL actionMethod = selector;
    CGSize textSize = [title?:@"" sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) fontSize:14];
    
    UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width > 60 ? 60 : textSize.width, 44)];
    shareButton.tag = 1001;
    shareButton.backgroundColor = [UIColor clearColor];
    shareButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [shareButton setTitle:title forState:UIControlStateNormal];
    [shareButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shareButton addTarget:owner
                    action:actionMethod
          forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *replyItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
    
    self.rightBarButtonItem = replyItem;
    
}

- (void)addRightNavItem:(id)owner title:(NSString *)title selector:(SEL)selector
{
    SEL actionMethod = selector;
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65.0f, 44)];
    btn.tag = 1001;
    btn.backgroundColor = [UIColor clearColor];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:owner
            action:actionMethod
  forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.rightBarButtonItem = btnItem;
    
}

- (UIButton *)addRightNavItem:(id)owner normalImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage selector:(SEL)selector
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:normalImage forState:UIControlStateNormal];
    if (highlightedImage) {
        [button setImage:highlightedImage forState:UIControlStateHighlighted];
    }
    [button setFrame:CGRectMake(0, 0, 40, 40)];
    [button addTarget:owner action:selector forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.rightBarButtonItem = item;
    return button;
}
@end
